package com.example.administrator.yuejian2.fragment;


import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.Toast;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.alibaba.android.vlayout.layout.GridLayoutHelper;
import com.alibaba.android.vlayout.layout.LinearLayoutHelper;
import com.alibaba.android.vlayout.layout.SingleLayoutHelper;
import com.alibaba.android.vlayout.layout.StickyLayoutHelper;
import com.example.administrator.yuejian2.R;
import com.example.administrator.yuejian2.RetrofitListener.RetrofitListener;
import com.example.administrator.yuejian2.VLayoutAdapter.BannerAdapter;
import com.example.administrator.yuejian2.VLayoutAdapter.GridAdapter;
import com.example.administrator.yuejian2.VLayoutAdapter.StickyAdapter;
import com.example.administrator.yuejian2.adapter.ActivityAdapter;
import com.example.administrator.yuejian2.adapter.ClubAdapter;
import com.example.administrator.yuejian2.adapter.VLayoutClubAdapter;
import com.example.administrator.yuejian2.adapter.mFragmentPagerAdapter;
import com.example.administrator.yuejian2.bean.ActivityBean;
import com.example.administrator.yuejian2.model.ActivityModel;

import java.util.LinkedList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 */
public class VLayoutFragment extends Fragment {

    private RecyclerView recyclerView;
    public BannerAdapter bannerAdapter;
    public GridAdapter gridAdapter;
    public StickyAdapter stickyAdapter;
    public ActivityAdapter activityAdapter;
    public RetrofitListener<List<ActivityBean>> listener;
    public DelegateAdapter delegateAdapter;
    public VLayoutClubAdapter clubAdapter;
    public VLayoutFragment() {
        // Required empty public constructor
    }

    private void initView(View view) {
        recyclerView = view.findViewById(R.id.vlayout_fragment);
        VirtualLayoutManager virtualLayoutManager = new VirtualLayoutManager(getContext());
        recyclerView.setLayoutManager(virtualLayoutManager);
        //设置view缓存个数
        final RecyclerView.RecycledViewPool viewPool = new RecyclerView.RecycledViewPool();
        recyclerView.setRecycledViewPool(viewPool);
        viewPool.setMaxRecycledViews(0, 20);
        //RecylerView创建适配器，初始化DelegateAdapter
         delegateAdapter = new DelegateAdapter(virtualLayoutManager, false);
        recyclerView.setAdapter(delegateAdapter);
        List<DelegateAdapter.Adapter> adapters = new LinkedList<>();
        delegateAdapter.addAdapters(adapters);
        bannerAdapter = new BannerAdapter(getContext(), new SingleLayoutHelper());
        gridAdapter = new GridAdapter(getContext(), new GridLayoutHelper(4));
        stickyAdapter = new StickyAdapter(getContext(), new StickyLayoutHelper(true));
        activityAdapter = new ActivityAdapter(getContext(), new LinearLayoutHelper(5));
       clubAdapter= new VLayoutClubAdapter(getContext(), new LinearLayoutHelper(5));
        delegateAdapter.addAdapter(bannerAdapter);
        delegateAdapter.addAdapter(gridAdapter);
        delegateAdapter.addAdapter(stickyAdapter);
        delegateAdapter.addAdapter(activityAdapter);

    }


    private void getactivity() {
        ActivityModel activityModel = new ActivityModel();
        activityModel.activityList(listener);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        listener = new RetrofitListener<List<ActivityBean>>() {
            @Override
            public void onSuccess(List<ActivityBean> list, int flag) {
                activityAdapter.activityData(list);
            }

            @Override
            public void onFail() {

            }
        };
        View v = inflater.inflate(R.layout.fragment_vlayout, container, false);
        initView(v);
        getactivity();
        return v;
    }


}
